package com.laikan.reader.bookfriend.activity;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.reader.R;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SignLuckyRecod extends PopupWindow {

    private View mMenuView;
    private TextView lucky_name, lucky_date;
    private RelativeLayout have_lucky, no_lucky;

    public SignLuckyRecod(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.signluckyrecod, null);
        //取消按钮
        //设置按钮监听
        RelativeLayout redpackage = (RelativeLayout)mMenuView.findViewById(R.id.redpackage);
        lucky_name = (TextView)mMenuView.findViewById(R.id.lucky_name);
        lucky_date = (TextView)mMenuView.findViewById(R.id.lucky_date);

        have_lucky = (RelativeLayout)mMenuView.findViewById(R.id.have_lucky);
        no_lucky = (RelativeLayout)mMenuView.findViewById(R.id.no_lucky);
        //        String format = String.format(context.getString(R.string.sendQuan), b.getShareReadSendQuan());
        //        tv_quan.setText(format);

        //        startShakeAnimation(redpackage);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //		this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xa0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int bottom = mMenuView.findViewById(R.id.pop_layout).getBottom();
                int y = (int)event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }else if (y > bottom) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

    public void setContentView(String name, String dates) {
        have_lucky.setVisibility(View.VISIBLE);
        lucky_name.setText(name);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd HH:mm");
        String sd = sdf.format(new Date(Long.parseLong(dates)));
        System.out.println(sd);
        lucky_date.setText("(获奖时间: "+sd+")");
    }

    public void setContentViewNo() {
        no_lucky.setVisibility(View.VISIBLE);

    }

    private void startShakeAnimation(View v) {
        int pivot = Animation.RELATIVE_TO_SELF;
        //        CycleInterpolator interpolator = new CycleInterpolator(3.0f);
        RotateAnimation animation = new RotateAnimation(6, -6, pivot, 0.5f, pivot, 0.5f);
        //        animation.setStartOffset(1000);
        animation.setDuration(200);
        animation.setRepeatCount(1);
        //        animation.setInterpolator(interpolator);
        v.startAnimation(animation);
    }
}
